<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/13
 * Time: 9:37
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\GoodsModel;

class GoodsController extends AdminBaseController
{

    //商品列表
    public function goodsList()
    {
        //判断点击的是分类还是全部
        $param = $this->request->param();
        if ($param) {
            $fenlei = $param['fenlei'];
            if (!($fenlei == "0")) {
                //有分类
                $goodemodel = new GoodsModel();
                $data = $goodemodel->goodslist($fenlei);
                $types = $goodemodel->type();
                $this->assign('types', $types);
                $this->assign('data', $data);
                return $this->fetch();
            }
        }
        $goodemodel = new GoodsModel();
        $data = $goodemodel->goodslist("0");
        $types = $goodemodel->type();
        $this->assign('types', $types);
        $this->assign('data', $data);
        return $this->fetch();

    }

    //商品添加
    public function add()
    {
        $param = $this->request->param();

        $goodemodel = new GoodsModel();


        $types = $goodemodel->type();

        $this->assign('types', $types);
        return $this->fetch();
    }


    public function edit()
    {
        $param = $this->request->param();
        $goodemodel = new GoodsModel();

        if ($param) {

            $data = $goodemodel->xiugaichaxun($param['goods_id']);
            $types = $goodemodel->type();

            $this->assign('types', $types);

            $this->assign('data', $data);
            $this->assign('biaoti', '修改商品');
            $this->assign('btn', '修改');
        }

        $types = $goodemodel->type();

        $this->assign('types', $types);
        return $this->fetch();
    }

    //商品添加成功 修改
    public function addsuccess()
    {
        $param = $this->request->param();

        $file = request()->file('image');
        $image = $file;
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'admin');
            if ($info) {
                $image = $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

        if ($param) {
            $goodemodel = new GoodsModel();
            $res = $goodemodel->add($param, $image);
            if ($res == true) {
                $this->success('添加成功', 'goods/goodsList');
            } else {
                $this->error('添加失败');
            }
        }


    }


    //商品 修改成功
    public function editsuccess()
    {
        $param = $this->request->param();

        $file = request()->file('image');
        $image = $file;

        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'admin');
            if ($info) {
                $image = $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

        if ($param) {
            $goodemodel = new GoodsModel();
            $res = $goodemodel->add($param, $image);
            if ($res == true) {
                $this->success('修改成功', 'goods/goodsList');
            } else {
                $this->error('修改失败');
            }
        }

    }

    public function goodsdel()
    {
        $param = $this->request->param();
        $goodemodel = new GoodsModel();
        $res = $goodemodel->goodsdel($param);
        if ($res == true) {
            $this->success('删除成功', 'goods/goodsList');
        } else {
            $this->error('删除失败');
        }
    }

}